package com.printeverywhere.delegator;

import static com.printeverywhere.util.AdminConstants.CALLED_ADD_METHOD;
import static com.printeverywhere.util.AdminConstants.CALLED_UPDATE_METHOD;
import static com.printeverywhere.util.AdminConstants.ERROR_GET_LIST_WITHOUT_SEARCH_CRITERIA;
import static com.printeverywhere.util.AdminConstants.ERROR_IN_ADDING_THE_RECORD;
import static com.printeverywhere.util.AdminConstants.ERROR_IN_UPDATING_THE_RECORD;
import static com.printeverywhere.util.AdminConstants.GET_LIST_WITHOUT_SEARCH_CRITERIA;
import static com.printeverywhere.util.AdminConstants.RECORD_ADDED;
import static com.printeverywhere.util.AdminConstants.RECORD_UPDATED;
import static com.printeverywhere.util.AdminConstants.SESSION_CLOSED;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.printeverywhere.dto.CustomFields;
import com.printeverywhere.dto.GeneralCategories;
import com.printeverywhere.dto.LocationCategories;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.HibernateUtil;

public class CustomFieldsFormatsDelegator {
	private static Logger logger = Logger
			.getLogger(CustomFieldsFormatsDelegator.class);

	public static String add(CustomFields customFieldsFormats)
			throws HibernateException {
		logger.info(CALLED_ADD_METHOD);
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		try {
			session.save(customFieldsFormats);
			transaction.commit();
			logger.info(RECORD_ADDED);
			return RECORD_ADDED;
		} catch (HibernateException e) {
			logger.error(ERROR_IN_ADDING_THE_RECORD);
			transaction.rollback();
			throw e;
		} finally {
			logger.info(SESSION_CLOSED);
			session.close();
		}
	}

	public static String update(CustomFields customFieldsFormats)
			throws HibernateException {
		logger.info(CALLED_UPDATE_METHOD);
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		try {
			session.update(customFieldsFormats);
			transaction.commit();
			logger.info(RECORD_UPDATED);
			return RECORD_UPDATED;
		} catch (HibernateException e) {
			logger.error(ERROR_IN_UPDATING_THE_RECORD);
			transaction.rollback();
			throw e;
		} finally {
			logger.info(SESSION_CLOSED);
			session.close();
		}
	}

	public static String delete(CustomFields customFieldsFormats)
			throws HibernateException {
		logger.info(AdminConstants.CALLED_DELETE_METHOD);
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		try {
			session.delete(customFieldsFormats);
			transaction.commit();
			logger.info(AdminConstants.RECORD_DELETED);
			return AdminConstants.RECORD_DELETED;
		} catch (HibernateException e) {
			logger.error(AdminConstants.ERROR_IN_DELETING_THE_RECORD);
			transaction.rollback();
			throw e;
		} finally {
			logger.info(SESSION_CLOSED);
			session.close();
		}
	}
	public static List<CustomFields> getList() throws HibernateException {
		logger.info(GET_LIST_WITHOUT_SEARCH_CRITERIA);
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		try {
			List<CustomFields> list = session.createCriteria(
					CustomFields.class).list();
			transaction.commit();
			return list;
		} catch (HibernateException e) {
			logger.error(ERROR_GET_LIST_WITHOUT_SEARCH_CRITERIA
					+ e.getMessage());
			transaction.rollback();
			throw e;
		} finally {
			logger.info(SESSION_CLOSED);
			session.close();
		}
	}

}
